package code.Frame.EPFrame;

import code.Frame.FrameManager;
import code.MYSQL;
import code.Main;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class mainFrame {
    public JPanel panel;
    private JLabel title;
    private JButton enterprise;
    private JButton shop;
    private JButton back;
    private JButton orders;

    public boolean start;

    public mainFrame() {
        back.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                Main.frame.remove(Main.panel_now);
                Main.panel_now = FrameManager.startPanel;
                start = false;
                Main.frame.setContentPane(Main.panel_now);
                Main.panel_now.revalidate();
            }
        });
        enterprise.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                Main.frame.remove(Main.panel_now);
                Main.panel_now = FrameManager.ep_info;
                FrameManager.ep_infoFrame.refresh_list();
                Main.frame.setContentPane(Main.panel_now);
                Main.panel_now.revalidate();
            }
        });
        shop.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                Main.frame.remove(Main.panel_now);
                Main.panel_now = FrameManager.ep_shop;
                FrameManager.shop.refresh_table();
                Main.frame.setContentPane(Main.panel_now);
                Main.panel_now.revalidate();
            }
        });
        orders.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                Main.frame.remove(Main.panel_now);
                Main.panel_now = FrameManager.ep_process;
                FrameManager.ep_processFrame.start_auto_fresh();
                FrameManager.ep_processFrame.refresh_table();
                Main.frame.setContentPane(Main.panel_now);
                Main.panel_now.revalidate();
            }
        });
    }

    public void start_check()
    {
        start = true;
        Thread check = new Thread(new Runnable() {
        @Override
            public void run()
            {
                while (start)
                {
                    try
                    {
                        if (MYSQL.is_incomplete(Main.uid - 200000000))
                            orders.setBackground(Color.red);
                        else
                            orders.setBackground(Color.WHITE);
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                    try
                    {
                        for (int i = 0; i<10 && start; i++)
                            Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        check.start();
    }

}
